#!/bin/bash
# Usage: ./set_mcfm_parameter.sh nproc 141
# Usage: ./set_mcfm_parameter.sh muR 173d0
# Usage: ./set_mcfm_parameter.sh muF 173d0
# Usage: ./set_mcfm_parameter.sh mt 173d0
# Usage: ./set_mcfm_parameter.sh s 8000d0
# Usage: ./set_mcfm_parameter.sh name \'myrun\'
# Usage: ./set_mcfm_parameter.sh order \'lord\'
# Usage: ./set_mcfm_parameter.sh order \'tota\'
#
# the target file has the structure: 
#
# 31 [nproc]
#

##################################
source ~/scriptbelt/bashutilities.sh
##################################

basefile=input.DAT
verbose=false

for iarg in $*; do
    CLIinputs[$argCounter]=$iarg
    option=`echo "$iarg" | cut -d"=" -f1`

   if [ "$option" == "--file" ]; then
	basefile=`echo "$iarg" | cut -d"=" -f2`
    fi
    if [ "$option" == "-v" ]; then
	verbose=true
	fi	
    if [ "$option" == "-h" ]; then
	echo "Usage: ./set_mcfm_parameter.sh nproc 141"
	echo "Usage: ./set_mcfm_parameter.sh Rcut_jet 0.5d0"
	echo "Usage: ./set_mcfm_parameter.sh ncall1 100000"
	echo "Usage: ./set_mcfm_parameter.sh ncall2 100000"
	echo "Usage: ./set_mcfm_parameter.sh muR 173d0"
	echo "Usage: ./set_mcfm_parameter.sh muF 173d0"
	echo "Usage: ./set_mcfm_parameter.sh mt 173d0"
	echo "Usage: ./set_mcfm_parameter.sh s 8000d0"
	echo "Usage: ./set_mcfm_parameter.sh name \'myrun\'"
	echo "Usage: ./set_mcfm_parameter.sh order \'lord\'"
	echo "Usage: ./set_mcfm_parameter.sh order \'tota\'"
	exit
    fi


done

#cp -v "$basefile" "$basefile".backup
echo "Modifying file " $basefile


num=$2

param=$1

if [ "$1" = "muF" ]; then
echo "Changing the FACTORIZATION scale"
param="facscale\:QCD\ fac\_scale\ choice"
fi
if [ "$1" = "muR" ]; then
echo "Changing the RENORMALIZATION scale"
param=scale\:QCD\ scale\ choice
fi
if [ "$1" = "mt" ]; then
echo "Changing mtop"
param=top\ mass
fi
if [ "$1" = "s" ]; then
param=sqrts\ in\ GeV
fi
if [ "$1" = "name" ]; then
param=\'runstring\'
fi
if [ "$1" = "order" ]; then
param=part\ \'lord\'\,\'real\'\ or\ \'virt\'\,\'tota\'
fi



echo "Changing the file "$basefile
echo $num" ["$param"]"

#take whatever line contains ['runstring'] and substitute it with a whole new line made of $2 ['runstring']
cat $basefile | sed "s/.*\[$param\].*/$num\ \[$param\]/g" > temptemp
cp $basefile $basefile.backup
cp temptemp $basefile
rm temptemp
if [ "$verbose" = true ]; then
 echo -e $RED"diff -y --suppress-common-lines "$basefile".backup $basefile"
 diff -y --suppress-common-lines "$basefile".backup $basefile
 echo -e $COL_RESET
fi
